
let id = localStorage.getItem('id')
let showAPI = 'http://jx.xuzhixiang.top/ap/api/cart-list.php'

$.get(showAPI, {
    id,
}).then(res => {
    let pobj = res.data
    if (pobj.length == 0) {
        $('.cartdie').show()
        $('.cartshow').hide()
    } else {
        $('.cartdie').hide()
        $('.cartshow').show()
    }

    $.each(pobj, function (i, v) {
        let html = `<div class="cart-list">
        <p class="ipt-p"><input type="checkbox" name="" id="" class="less" data-id="${v.pid}"></p>
        <p class="img-p"><img src="${v.pimg}" alt=""></p>
        <p class="name-p">${v.pname}</p>
        <p class="less-p">${v.pprice}</p>
        <p class="num-p">
            <input type="button" value="-" class="sub-btn" data-id="${v.pid}">
            <input type="text" value="${v.pnum}" class="num-ipt">
            <input type="button" value="+" class="add-btn" data-id="${v.pid}">
        </p>
        <p class="small-p" data-id="${v.pid}"></p>
        <p class="delete-p" data-id="${v.pid}">删除</p>
</div>`
        $(`<div>${html}</div>`).appendTo('.cartshow')

        // 删除事件
        $('.delete-p').click(function () {
            let pid = $(this).attr('data-id')
            let deleteAPI = 'http://jx.xuzhixiang.top/ap/api/cart-delete.php'

            $.get(deleteAPI, {
                uid: id,
                pid,
            }).then(res => {
                $(this).parent().remove()
                $.get(showAPI, {
                    id,
                }).then(res => {
                    let pobj = res.data
                    if (pobj.length == 0) {
                        $('.cartdie').show()
                        $('.cartshow').hide()
                    } else {
                        $('.cartdie').hide()
                        $('.cartshow').show()
                    }
                })
            })
        })

        // 增加数量
        // $('.add-btn').click(function () {
        //     let pid = $(this).attr('data-id')
        //     let obj = pobj.find(v => v.pid == pid)
        //     obj.pnum++
        //     $(this).prev().val(obj.pnum)
        //     let addAPI = 'http://jx.xuzhixiang.top/ap/api/cart-update-num.php'

        //     $.get(addAPI, {
        //         pid,
        //         uid: id,
        //         pnum: obj.pnum,
        //     }).then(res => {

        //     })
        // })

        // 增加数量
        let addBtns = document.querySelectorAll('.add-btn')
        for (let i = 0; i < addBtns.length; i++) {
            addBtns[i].onclick = function () {
                let pid = $(this).attr('data-id')
                let obj = pobj.find(v => v.pid == pid)
                obj.pnum++
                this.previousElementSibling.value = obj.pnum
                let addAPI = 'http://jx.xuzhixiang.top/ap/api/cart-update-num.php'

                $.get(addAPI, {
                    pid,
                    uid: id,
                    pnum: obj.pnum,
                }).then(res => {

                })
            }

        }

        // 减少数量
        let subBtns = document.querySelectorAll('.sub-btn')
        for (let i = 0; i < subBtns.length; i++) {
            subBtns[i].onclick = function () {
                let pid = $(this).attr('data-id')
                let obj = pobj.find(v => v.pid == pid)
                if (obj.pnum == 1) {
                    return
                }
                obj.pnum--
                this.nextElementSibling.value = obj.pnum
                let addAPI = 'http://jx.xuzhixiang.top/ap/api/cart-update-num.php'

                $.get(addAPI, {
                    pid,
                    uid: id,
                    pnum: obj.pnum,
                }).then(res => {

                })
            }

        }


        // 减少数量
        // $('.sub-btn').click(function () {
        //     let pid = $(this).attr('data-id')
        //     let obj = pobj.find(v => v.pid == pid)
        //     if (obj.pnum == 1) {
        //         return
        //     }
        //     obj.pnum--
        //     $(this).next().val(obj.pnum)
        //     let addAPI = 'http://jx.xuzhixiang.top/ap/api/cart-update-num.php'

        //     $.get(addAPI, {
        //         pid,
        //         uid: id,
        //         pnum: obj.pnum,
        //     }).then(res => {

        //     })
        // })

        // 全选框
        $('#more').change(function () {
            $('.less').prop('checked', $(this).prop('checked'))
            // 调用计算总价函数
            resNum()
        })

        // 单选框
        $('.less').change(function () {
            let pid = $(this).attr('data-id')
            let obj = pobj.find(v => v.pid == pid)
            obj.isSelected = $(this).checked
            let lessArr = Array.from($('.less'))
            let moreCheck = lessArr.every(v => v.checked == true)

            $('#more').prop('checked', moreCheck)
            // 调用计算总价函数
            resNum()
        })

        // 计算总价函数封装
        function resNum() {
            let num = 0
            $('.less').each(function () {
                
                if ($(this).prop('checked') == true) {
                    num += $(this).parent().nextAll('.less-p').html() * $(this).parent().nextAll('.num-p').children('.num-ipt').val()
                }
                $('#resnum').html(num)
            })
        }
    })
})